import * as XLSX from 'xlsx'
/**
 * 前端导出功能 (中文方法内部处理了Descr)
 * @param list 表格数据
 * @param tableHeaders 表头config
 * @param title 导出文件标题
 */
export const exportXl = (list: any, tableHeaders: any, title: string) => {
  const header = tableHeaders.filter((v: any) => v.label != '操作').map((v: any) => v.label)
  const lists = list.map((v: any) => {
    const obj = {} as any
    tableHeaders.forEach((item: any) => {
      if (item['label'] == '操作') return
      if (typeof v[item['prop']] === 'boolean') {
        obj[item['label']] = v[item['prop']] ? '是' : '否'
      } else {
        obj[item['label']] = v[item.prop + 'Descr']
          ? v[item['prop']] + '-' + v[item.prop + 'Descr']
          : v[item['prop']]
      }
    })
    return obj
  })
  const data = XLSX.utils.json_to_sheet(lists, { header: header })
  data['!cols'] = tableHeaders
    .filter((v: any) => v.label != '操作')
    .map((v: any) => ({ wpx: v.width ? Number(v.width) - 40 : 60 }))
  const wb = XLSX.utils.book_new()
  XLSX.utils.book_append_sheet(wb, data, 'Sheet1')
  XLSX.writeFile(wb, `${title}.xlsx`)
}
